﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Study.LINQ;

namespace ProjectStudy.LINQ
{
    public partial class Linq : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //简单查询
            string[] arr1 = { "C#","ASP.NET","Json","Ajax"};
            IEnumerable<string> result = from string str in arr1
                                         orderby str ascending
                                         select str;
            foreach (string s in result)
            {
                Label1.Text = Label1.Text + s + "<br/>";
            }

            //简单对象查询
            ArrayList arr2 = new ArrayList();
            Student s1 = new Student("张三",20);
            Student s2 = new Student("李四", 20);
            Student s3 = new Student("王五", 21);
            arr2.Add(s1);
            arr2.Add(s2);
            arr2.Add(s3);
            var query = from Student s in arr2
                        where s.Age >= 20
                        select s;
            foreach (Student stu in query)
            {
                Label2.Text = Label2.Text + stu.Name + "," + stu.Age + "<br/>";
            }

            List<Student> liststu = new List<Student>();
            liststu.Add(s1);
            liststu.Add(s2);
            liststu.Add(s3);
            var res = from Student stud in liststu
                      orderby stud.Age ascending
                      group stud by stud.Age;
            foreach (var ele in res)
            {
                Label2.Text =Label2.Text+ ele.Key + "岁的学生有:";
                foreach (Student stud in ele)
                {
                    Label2.Text = Label2.Text + stud.Name + " ";
                }
                Label2.Text = Label2.Text + "<br/>";
            }

            //复合查询
            List<string> list1 = new List<string>();
            list1.Add("zhangsan");
            list1.Add("lisi");
            list1.Add("wangwu");

            List<string> list2 = new List<string>();
            list2.Add("zhangsan@qq.com");
            list2.Add("lisi@qq.com");
            list2.Add("wangwu@qq.com");

            var query2 = from string name in list1
                         from string email in list2
                         where email.Contains(name)
                         select name;
            foreach (string n in query2)
            {
                Label3.Text = Label3.Text + n + "<br/>";
            }

            DataClasses1DataContext db = new DataClasses1DataContext();
            var class1 = from cl in db.class1
                         select cl;
            GridView1.DataSource = class1;
            GridView1.DataBind();
            List<Object> cla = new List<object>();
            
        }
    }
}